package com.xywy.me;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneNumberUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;

/**
 * 我 - 设置 - 账户 - 手机号码 -验证手机号
 * 
 * @author xiaogd
 * 
 */
public class SetupAcValMobileActivity extends BaseActivity {
	private static final String TAG = "SetupAcValMobileActivity";
	private Context mContext;
	private String mobile;
	private TextView tvMobile;
	private EditText etMoible;
	private ImageView mCancel;
	private Button button;
	private RelativeLayout rl_prompt;
	private String userid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.me_setup_ac_val_mobile);

		this.preInit();
		this.initView();
		this.loadData();
		this.setListener();
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	public void preInit() {
		mContext = this.context;
		Intent intent = this.getIntent();
		// mobile = intent.getStringExtra("mobile");
		userid = intent.getStringExtra("userid");
	}

	@Override
	public void loadData() {

	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.me_setup_ac_valmobile_title_bar);
		titleBar.onClickBack(this);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		tvTitle.setText(getString(R.string.me_setup_account_valsj));
		tvBack.setText(getString(R.string.tv_start_back));
		tvBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SetupAcValMobileActivity.this.finish();
			}
		});
		rl_prompt = (RelativeLayout) findViewById(R.id.rl_prompt);
		tvMobile = (TextView) findViewById(R.id.tv_me_setup_ac_valsj_tv2_2);
		tvMobile.setText(mobile);

		etMoible = (EditText) findViewById(R.id.et_setup_ac_val_mobile);
		mCancel = (ImageView) findViewById(R.id.iv_setup_ac_val_mobile_mcancel);
		button = (Button) findViewById(R.id.bt_setup_ac_send_valcode);
	}

	public void startOnClick(View view) {
		switch (view.getId()) {
		case R.id.bt_setup_ac_send_valcode:
			if (isMobileNO(etMoible.getText().toString())) {
				next();
			} else {
				Toast.makeText(mContext, "请输入正确的手机号", Toast.LENGTH_SHORT)
						.show();
			}

			break;
		case R.id.iv_setup_ac_val_mobile_mcancel:
			etMoible.setText("");
		}
		;
	}

	public static boolean isMobileNO(String mobiles) {
		// Pattern p = Pattern
		// .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		// Matcher m = p.matcher(mobiles);
		PhoneNumberUtils.isGlobalPhoneNumber(mobiles);
		System.out.println(PhoneNumberUtils.isGlobalPhoneNumber(mobiles)
				+ "---测试");
		return PhoneNumberUtils.isGlobalPhoneNumber(mobiles);
	}

	private void next() {
		sendMsg(); // 发送网络请求
	}

	private void setListener() {
		etMoible.addTextChangedListener(mTextWatcher);
	}

	TextWatcher mTextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence charSequence, int i, int i2,
				int i3) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void afterTextChanged(Editable editable) {
			if (editable.length() > 0) {
				mCancel.setVisibility(View.VISIBLE);
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button));
				button.setClickable(true);
			} else {
				mCancel.setVisibility(View.INVISIBLE);
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button_bgwhite));
				button.setClickable(false);
			}
		}
	};

	MyProgress myProgress;
	JSONObject msgBack = null;
	/**
	 * 返回
	 */
	private Handler handler = new Handler() {
	};

	// 封装Parser参数
	private Parser paramsParser;
	// 封装httpclient参数
	private HashMap<String, Object> params;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求
	public void sendMsg() {
		myProgress = new MyProgress(this, 10);
		myProgress.start();
		paramsParser = new Parser();

		// 封装参数
		params = new HashMap<String, Object>();

		// 请求路径
		paramsParser.action = getString(R.string.validate_sendCode);
		// 请求方式
		paramsParser.request = "get";

		// 请求参数
		params.put("target", etMoible.getText().toString());
		// params.put("userid",);
		params.put("target", etMoible.getText().toString());

		params.put("type", ConstantValue.BINDACCOUNT);

		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.context;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = Message.obtain();
			message.arg1 = resultCode;
			message.obj = resultParser.back;
			handler.sendMessage(message);
		}
	};

}
